cmake_minimum_required(VERSION 3.8)
project(usv_controller)

# 设置C++标准
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

# 查找ROS2依赖包
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(mavros_msgs REQUIRED)
find_package(custom_msgs REQUIRED)

#========================
# 构建控制库
#========================

# 创建控制算法库
add_library(${PROJECT_NAME}_lib SHARED
  src/motion_plan.cpp
  src/motion_control.cpp
)

# 设置库的编译定义
target_compile_definitions(${PROJECT_NAME}_lib
  PRIVATE "USV_CONTROLLER_BUILDING_LIBRARY")

# 设置库的包含目录 - 使用现代CMake方式
target_include_directories(${PROJECT_NAME}_lib
  PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

# 设置库的依赖
ament_target_dependencies(${PROJECT_NAME}_lib
  rclcpp
  std_msgs
  sensor_msgs
  geometry_msgs
  mavros_msgs
  custom_msgs
)

#========================
# 构建可执行文件
#========================

# 主控制节点 - 链接库后会自动继承PUBLIC包含目录
add_executable(${PROJECT_NAME}_node 
  src/usv_controller.cpp
)

# 链接库 - 会自动继承库的PUBLIC包含目录和依赖
target_link_libraries(${PROJECT_NAME}_node
  ${PROJECT_NAME}_lib
)

# 注意：由于链接了库，这里不需要重复设置包含目录和依赖
# 库的PUBLIC设置会自动传递给可执行文件

#========================
# 安装配置
#========================

# 安装库文件
install(TARGETS ${PROJECT_NAME}_lib
  EXPORT ${PROJECT_NAME}_libTargets
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

# 安装可执行文件
install(TARGETS ${PROJECT_NAME}_node
  DESTINATION lib/${PROJECT_NAME}
)

# 安装头文件
install(DIRECTORY include/
  DESTINATION include/
  FILES_MATCHING PATTERN "*.h"
)

# 安装启动文件 - 修复：添加权限设置
install(DIRECTORY launch/
  DESTINATION share/${PROJECT_NAME}/launch/
  FILES_MATCHING PATTERN "*.py"
  PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
              GROUP_READ GROUP_EXECUTE
              WORLD_READ WORLD_EXECUTE
)

# 安装配置文件
install(DIRECTORY config/
  DESTINATION share/${PROJECT_NAME}/config/
  FILES_MATCHING PATTERN "*.yaml"
)

#========================
# 测试配置
#========================

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # 跳过一些不必要的检查
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
  
  # 可选：添加单元测试
  find_package(ament_cmake_gtest REQUIRED)
  
  # 如果有测试文件，可以在这里添加
  # ament_add_gtest(${PROJECT_NAME}_test test/test_usv_controller.cpp)
  # if(TARGET ${PROJECT_NAME}_test)
  #   target_link_libraries(${PROJECT_NAME}_test ${PROJECT_NAME}_lib)
  #   ament_target_dependencies(${PROJECT_NAME}_test
  #     rclcpp
  #     std_msgs
  #     sensor_msgs
  #     geometry_msgs
  #   )
  # endif()
endif()

#========================
# ament包配置
#========================

# 导出库目标 - 修复：添加库目标导出
install(EXPORT ${PROJECT_NAME}_libTargets
  FILE ${PROJECT_NAME}_libTargets.cmake
  NAMESPACE ${PROJECT_NAME}::
  DESTINATION lib/cmake/${PROJECT_NAME}
)

# 导出依赖
ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME}_lib)
ament_export_targets(${PROJECT_NAME}_libTargets HAS_LIBRARY_TARGET)
ament_export_dependencies(
  rclcpp
  std_msgs
  sensor_msgs
  geometry_msgs
)

ament_package()